<?php

namespace App\Http\Controllers\Admin;

use App\Models\Agency;
use App\Models\Child;
use App\Models\Classes;
use App\Models\Family;
use App\Models\Student;
use App\Models\User;
use Illuminate\Http\Request;

class StudentController extends BaseController
{
    /**
     * 按照平台和学校区分不同，展示不同范围的数据，
     * 显示形式相同。
     *
     * 平台展示所有
     * 学校展示本校学生
     *
     */

    public function index(Request $request)
    {
        $classes = [];
        $query = $request->only('name', 'c_code');
        $student = new Student();
        $admin = $this->getAdmin();
        if(!empty($admin->tc_code)) {
            $class = Classes::where('tc_code', $admin->tc_code)->first();
            if (!empty($class)) {
                $student = $student->where('c_code', $class->c_code);
            } else {
                $student = $student->where('c_code', '0');
            }
        } else {
            $classes = Classes::all();
        }
        if(!empty($query['name'])) {
            $student = $student->where('name', 'like', '%'.$query['name'].'%');
        }
        if(!empty($query['c_code'])) {
            $student = $student->where('c_code', $query['c_code']);
        }
        $list = $student->orderBy('id', 'desc')->paginate(config('site.other.paginate'));
        $list = $list->appends($request->input());
        return backendView('student.list', compact('list', 'classes','query'));
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'name' => 'required|max:255',
//                'birthday' => 'required|date',
//                'sex' => 'required|in:0,1',
                'c_code' => 'required',
                's_code' => 'required',
            ], [], [
                'name' => __("Username"),
//                'birthday' => __("Birthday"),
//                'sex' => __("Sex"),
                'c_code' => '班级',
                's_code' => __("code"),
            ]);

            $data = $request->only(['name', 'c_code', 's_code']);
            if (Student::create($data)) {
                return redirect()->route('admin.student.add', ['c_code' => $data['c_code']])->with('success', __('Create success').'，继续添加此班级学生');
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            $classes = Classes::all();
            $query = $request->only('c_code');
            return backendView('student.add', compact('classes','query'));
        }
    }

    public function edit($id, Request $request)
    {
        $student = Student::find($id);
        if (empty($student)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod("post")) {
            $this->validate($request, [
                'name' => 'required|max:255',
//                'birthday' => 'required|date',
//                'sex' => 'required|in:0,1',
                'c_code' => 'required',
                's_code' => 'required',
            ], [], [
                'name' => __("Username"),
//                'birthday' => __("Birthday"),
//                'sex' => __("Sex"),
                'c_code' => '班级',
                's_code' => __("code"),
            ]);
            $data = $request->only(['name', 'c_code', 's_code']);

            if ($student->update($data)) {
                return redirect()->route('admin.student.index')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'));
        } else {
            $classes = Classes::all();
            return backendView('student.edit', compact('student','classes'));
        }
    }

    //假删除，
    public function delete($id)
    {
        $student = Student::find($id);
        if (empty($student)) {
            return back()->with('error', __('The page you requested was not found'));
        }
        try {
            if ($student->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }

    public function view($id)
    {
        $child = Child::withTrashed()->find($id);

        if (empty($child)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        return backendView('child.view', compact('child'));
    }

}
